import { createStore, compose, combineReducers } from 'redux';
import { applyMiddleware, log, thunk } from './middleware';

import counterReducer from './counter/reducer';
import homeReducer from './home/reducer';

// 正常情况下 store.dispatch(object)
// 想要派发函数 store.dispatch(function)
// 将两个reducer合并在一起
// combineReducers实现原理(了解)
// function reducer(state = {}, action) {
//   // 返回一个对象, store的state
//   return {
//     counter: counterReducer(state.counter, action),
//     home: homeReducer(state.home, action),
//     user: userReducer(state.user, action)
//   }
// }
// combineReducers: 合并多个reducer
const reducer = combineReducers({
  counter: counterReducer,
  home: homeReducer
})

// redux-devtools: 调试工具
// const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({trace: true}) || compose;

// 创建store
const store = createStore(reducer)

// applyMiddleware: 应用中间件合并执行, dispatch函数执行
applyMiddleware(store, log, thunk)

export default store;